package com.wmh.baseservice.common.mybatisplus.structure;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlKeyword;
import com.baomidou.mybatisplus.core.enums.SqlLike;
import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.wmh.baseservice.common.utils.CommUtil;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.Collection;

/**
 * 定制 QueryWrapper
 *
 * @author
 */
public class CustomQueryWrapper<T> extends QueryWrapper<T> {

    @Override
    protected QueryWrapper<T> addCondition(boolean condition, String column, SqlKeyword sqlKeyword, Object val) {
        // 忽略条件参数值为空的条件
        if (!CommUtil.checkNull(val)) {
            condition = false;
        }
        return super.addCondition(condition, column, sqlKeyword, val);
    }

    @Override
    protected QueryWrapper<T> likeValue(boolean condition, SqlKeyword keyword, String column, Object val, SqlLike sqlLike) {
        if (!CommUtil.checkNull(val)) {
            condition = false;
        }
        return super.likeValue(condition, keyword, column, val, sqlLike);
    }

    @Override
    public QueryWrapper<T> in(boolean condition, String column, Collection<?> coll) {
        if (coll.size() < 1) {
            condition = false;
        }
        return super.in(condition, column, coll);
    }

    @Override
    public QueryWrapper<T> notIn(boolean condition, String column, Collection<?> coll) {
        if (coll.size() < 1) {
            condition = false;
        }
        return super.notIn(condition, column, coll);
    }

    public QueryWrapper<T> likeIfPresent(String column, String val) {
        if (StringUtils.hasText(val)) {
            return super.like(column, val);
        }
        return this;
    }

    public QueryWrapper<T> inIfPresent(String column, Collection<?> values) {
        if (!CollectionUtils.isEmpty(values)) {
            return  super.in(column, values);
        }
        return this;
    }

    public QueryWrapper<T> inIfPresent(String column, Object... values) {
        if (!ArrayUtils.isEmpty(values)) {
            return  super.in(column, values);
        }
        return this;
    }

    public QueryWrapper<T> eqIfPresent(String column, Object val) {
        if (val != null) {
            return  super.eq(column, val);
        }
        return this;
    }

    public QueryWrapper<T> neIfPresent(String column, Object val) {
        if (val != null) {
            return  super.ne(column, val);
        }
        return this;
    }

    public QueryWrapper<T> gtIfPresent(String column, Object val) {
        if (val != null) {
            return  super.gt(column, val);
        }
        return this;
    }

    public QueryWrapper<T> geIfPresent(String column, Object val) {
        if (val != null) {
            return super.ge(column, val);
        }
        return this;
    }

    public QueryWrapper<T> ltIfPresent(String column, Object val) {
        if (val != null) {
            return  super.lt(column, val);
        }
        return this;
    }

    public QueryWrapper<T> leIfPresent(String column, Object val) {
        if (val != null) {
            return  super.le(column, val);
        }
        return this;
    }

    public QueryWrapper<T> betweenIfPresent(String column, Object val1, Object val2) {
        if (val1 != null && val2 != null) {
            return  super.between(column, val1, val2);
        }
        if (val1 != null) {
            return  ge(column, val1);
        }
        if (val2 != null) {
            return  le(column, val2);
        }
        return this;
    }

    public QueryWrapper<T> betweenIfPresent(String column, Object[] values) {
        if (values!= null && values.length != 0 && values[0] != null && values[1] != null) {
            return  super.between(column, values[0], values[1]);
        }
        if (values!= null && values.length != 0 && values[0] != null) {
            return  ge(column, values[0]);
        }
        if (values!= null && values.length != 0 && values[1] != null) {
            return  le(column, values[1]);
        }
        return this;
    }

    // ========== 重写父类方法，方便链式调用 ==========

    @Override
    public QueryWrapper<T> eq(boolean condition, String column, Object val) {
        super.eq(condition, column, val);
        return this;
    }

    @Override
    public QueryWrapper<T> eq(String column, Object val) {
        super.eq(column, val);
        return this;
    }

    @Override
    public QueryWrapper<T> orderByDesc(String column) {
        super.orderByDesc(true, column);
        return this;
    }

    @Override
    public QueryWrapper<T> last(String lastSql) {
        super.last(lastSql);
        return this;
    }

    @Override
    public QueryWrapper<T> in(String column, Collection<?> coll) {
        super.in(column, coll);
        return this;
    }

    public QueryWrapper<T> noSelect(Class<T> t, String... column) {
        return super.select(t, info -> Arrays.stream(column).noneMatch(c->info.getColumn().equals(c)));
    }

}
